package cn.zhibang.yygh.user.controller;

import cn.zhibang.yygh.common.handler.utils.IpUtil;
import cn.zhibang.yygh.common.result.Result;
import cn.zhibang.yygh.common.util.AuthContextHolder2;
import cn.zhibang.yygh.model.user.UserInfo;
import cn.zhibang.yygh.user.service.UserInfoService;
import cn.zhibang.yygh.vo.user.LoginVo;
import cn.zhibang.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {
    @Resource
    private UserInfoService userInfoService;
    @ApiOperation("会员登录")
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo, HttpServletRequest httpRequest)throws Exception{
        loginVo.setIp(IpUtil.getIpAddr(httpRequest));
        Map<String, Object> login = userInfoService.login(loginVo);
        return Result.ok(login);
    }
    //1.用户认证接口
    @ApiOperation(value = "用户认证接口")
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        //test
        String certificatesUrl = userAuthVo.getCertificatesUrl();
        System.out.println(certificatesUrl);
        boolean cardTest = userInfoService.IdCardTest(certificatesUrl);
        //传递两个参数，第一个参数用户id，第二个参数认证数据vo对象
        userInfoService.userAuth(AuthContextHolder2.getUserId(request),userAuthVo);
        if(cardTest){
            return Result.ok();
        }else{
            return Result.fail("请重新上传身份证");
        }
    }

    //2.获取用户id信息接口
    @ApiOperation(value = "获和用户ID接口")
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder2.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }
}
